<?php

namespace App\Task\Stat;

use App\Model\AppChannel;
use App\Model\CoinLog;
use App\Model\Stat\StatDailyCoin;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 每日统计-金币详情
 * @Crontab(name="StatDailyCoinTask", rule="3,23,43 *\/1 * * *", callback="execute", memo="每日金币详情统计", singleton=true)
 */
class StatDailyCoinTask
{
    public function execute()
    {
        // log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $this->runDaily();
        // for ($day = 30; $day > 0; $day--) {
        //     $this->runDaily($day);
        // }
    }

    function runDaily($day = 0)
    {
        $channels = []; //AppChannel::getAllChannels();
        if (time() - strtotime(date('Y-m-d')) < 300) {
            $day++;
        }
        $this->dt = date('Y-m-d', strtotime("-{$day} day"));
        $nextDay = $day - 1;
        $this->endTime = date('Y-m-d', strtotime("-{$nextDay} day"));
        $channels[] = 'all';
        foreach ($channels as $channel) {
            //日报表
            $this->beginStat($channel);
        }
    }

    /**
        $table->double('total_consume', 10, 1)->default(0.0)->comment('总消费金币');
        $table->double('video_consume', 10, 1)->default(0.0)->comment('视频消费金币');
        $table->double('voice_consume', 10, 1)->default(0.0)->comment('语音消费金币');
        $table->double('gift_consume', 10, 1)->default(0.0)->comment('礼物消费金币');
        $table->double('msg_consume', 10, 1)->default(0.0)->comment('私信消费金币');
        $table->double('other_consume', 10, 1)->default(0.0)->comment('其他消费金币');
     */
    function beginStat($channel = '')
    {
        $model = $this->getModel(StatDailyCoin::class, $channel);
        if (!$model) return false;
        $model->dt = $this->dt;
        $model->channel = $channel;
        $channel == 'all' && $channel = '';
        $model->video_consume = CoinLog::getConsumeCount($this->dt, $this->endTime, 'coin', $channel, [CoinLog::FROM_VIDEOCALL_OUT, CoinLog::FROM_QUICK_VIDEO_OUT]);
        $model->voice_consume = CoinLog::getConsumeCount($this->dt, $this->endTime, 'coin', $channel, [CoinLog::FROM_VOICECALL_OUT, CoinLog::FROM_QUICK_VOICE_OUT]);
        $model->msg_consume = CoinLog::getConsumeCount($this->dt, $this->endTime, 'coin', $channel, [CoinLog::FROM_SEND_PRIVATE_MSG_OUT]);
        $model->gift_consume = CoinLog::getConsumeCount($this->dt, $this->endTime, 'coin', $channel, [CoinLog::FROM_SEND_GIFT]);
        $model->total_consume = CoinLog::getConsumeCount($this->dt, $this->endTime, 'coin', $channel);
        $model->other_consume = $model->total_consume - $model->video_consume - $model->voice_consume - $model->gift_consume - $model->msg_consume;
        log_info(__METHOD__ . "=={$this->dt}=={$channel}");
        $model->saveOrFail();
    }

    /**
     * 获取模型
     */
    function getModel($statClass, $channel = '')
    {
        $where = ['dt' => $this->dt];
        if ($channel) $where['channel'] = $channel;
        $model = $statClass::where($where)->first();
        if (!$model) $model = new $statClass();
        return $model;
    }
}
